﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Security.Cryptography;

using X.Common;
using X.Config;

using E.Model;
using E.BLL;
using E.Enum;

namespace E.Web.UI.Page
{
    public class BasePage : System.Web.UI.Page
    {
        #region 属性

        /// <summary>
        /// 配置信息
        /// </summary>
        protected internal GeneralConfigInfo config;

        protected internal int userid;
        protected internal string username;
        protected internal int areaid;
        protected internal EnumUserAuthority authority;

        /// <summary>
        /// 提示文字
        /// </summary>
        protected internal string msgbox_text = "";

        /// <summary>
        /// 回退链接的内容
        /// </summary>
        protected internal string msgbox_backlink = "javascript:history.back();";

        protected internal string reffer = string.Empty;

        /// <summary>
        /// 当前页面是否被POST请求
        /// </summary>
        protected internal bool ispost;

        #region 分页相关

        protected internal string mypager = string.Empty;

        protected int count = 0;
        protected int pageIndex = 0;
        protected int currentPgGroup = 0;
        protected int customerPageSize = 0;
        protected int defaultPagesInGroup = 0;

        #endregion

        protected internal string pagename;
        private string pageFilter = "default,top,management,login,logout,feedbackframe,forgetpwd";

        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }

        public BasePage()
        {
            config = GeneralConfigs.GetConfig();

            userid = Utils.StrToInt(Utils.GetCookie("userid"), -1);
            username = Utils.GetCookie("username");
            areaid = Utils.StrToInt(Utils.GetCookie("areaid"), -1);
            authority = (EnumUserAuthority)Utils.StrToInt(Utils.GetCookie("ispublic"), 1);

            //userid = 66;
            //username = "i_s_1";
            //areaid = 1;
            //authority = EnumUserAuthority.Internal;

            //userid = 1;
            //username = "xxx";
            //areaid = 1;
            //authority = EnumUserAuthority.Public;

            pagename = GetPageName(XRequest.GetPageName());
            CheckLogin(pagename);

            ispost = XRequest.IsPost();
            customerPageSize = config.DefaultPageSize;
            defaultPagesInGroup = config.DefaultPagesInGroup;


            ShowPage();
        }
        
        /// <summary>
        /// 页面处理虚方法
        /// </summary>
        protected virtual void ShowPage()
        {
            return;
        }

        protected void Alert(string content)
        {
            ExecuteJS("alert('" + content + "');");
        }

        /// <summary>
        /// 执行javascript
        /// </summary>
        /// <param name="content">JS内容</param>
        protected void ExecuteJS(string content)
        {
            System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">");
            System.Web.HttpContext.Current.Response.Write(content);
            System.Web.HttpContext.Current.Response.Write("</script>");
        }

        protected string GetPageName(string requestPageName)
        {
            int requestPageNameLength = requestPageName.Length;
            return requestPageName.Substring(0, requestPageNameLength - 5);
        }

        protected IList<CourseCategoryModel> GetCourseCategoryList()
        {
            return (new CourseBLL()).GetCourseCategoryList();
        }

        /// <summary>
        /// 检查分页值
        /// </summary>
        /// <param name="_pageIndex">当前页</param>
        /// <param name="_currentPgGroup">当前分页组</param>
        /// <param name="_customerPageSize">每页记录数</param>
        protected void CheckPageIndexValue(int _pageIndex, int _currentPgGroup, int _customerPageSize)
        {
            if (_pageIndex > 0)
                pageIndex = _pageIndex;
            if (_currentPgGroup > 0)
                currentPgGroup = _currentPgGroup;
            if (_customerPageSize > 0)
                customerPageSize = _customerPageSize;
        }

        protected void Logout()
        {
            Utils.ClearCookie();
            ExecuteJS("alert('退出成功'); window.top.location= '/Default.aspx';");
        }

        /// <summary>
        /// 获取随机数组
        /// </summary>
        /// <param name="quantity">随机抽取数</param>
        /// <param name="list"></param>
        /// <returns></returns>
        protected IList<int> GetRandomValue(int quantity, IList<int> list)
        {
            IList<int> randomList = new List<int>();
            if (list.Count <= quantity)
            {
                foreach (int v in list)
                {
                    randomList.Add(v);
                }
            }
            else
            {
                for (int i = 0; i < quantity; i++)
                {
                    Random r = new Random();
                    int index = r.Next(0, list.Count);
                    int v = list[index];
                    randomList.Add(v);
                    list.Remove(v);
                    System.Threading.Thread.Sleep(50);
                }
            }
            return randomList;
        }

        private void CheckLogin(string pagename)
        {
            if (userid == -1)
            {
                if (pageFilter.IndexOf(pagename) == -1)
                {
                    ExecuteJS("alert('登录超时，请重新登录'); window.top.location = '/Default.aspx';");
                    return;
                }
            }
        }

        protected int GetCourseWarePageCount(int courseid)
        {
            return (new CourseBLL()).GetCourseWarePageCount(courseid);
        }

        protected int GetCurrentPageWithPercent(int percent, int maxpage)
        {
            double p = (double)((double)percent / 100);
            return Convert.ToInt32(Math.Round(maxpage * p, 0));
        }
    }
}